<template>
  <div class="mod-prod">
    <avue-crud ref="crud"
               :page="page"
               :data="dataList"
               :table-loading="dataListLoading"
               :option="tableOption"
               @search-change="searchChange"
               @on-load="getDataList">
      <template slot-scope="scope"
                slot="menu">

        <el-row v-if="scope.row.userType !== 2">
          <el-button class="button-spacing"
                     type="primary"
                     size="small"
                     @click="setManager(scope.row.id,1)">设置管理员</el-button>
        </el-row>
        <el-row v-if="scope.row.userType === 2">
          <el-button class="button-spacing"
                     type="danger"
                     size="small"
                     @click="setManager(scope.row.id,0)">取消管理员</el-button>
        </el-row>
        <el-row >
          <el-button class="button-spacing"
                     type="primary"
                     size="small"
                     @click="addIntegral(scope.row)">添加积分</el-button>
        </el-row>
        <el-row >
          <el-button class="button-spacing"
                     type="danger"
                     size="small"
                     @click="cleanIntegral(scope.row.id)">清空积分</el-button>
        </el-row>

      </template>
    </avue-crud>
  </div>
</template>

<script>
  import { tableOption } from '@/crud/wxUser/index'
  export default {
    name: 'index',
    data () {
      return {
        dataForm: {
          name: '',
          mobile: ''
        },
        dataList: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        dataListLoading: false,
        tableOption: tableOption
      }
    },
    methods: {
      // 获取数据列表
      getDataList (page, params) {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/v1/member/list'),
          method: 'get',
          params: this.$http.adornParams(
            Object.assign(
              {
                current: page == null ? this.page.currentPage : page.currentPage,
                size: page == null ? this.page.pageSize : page.pageSize
              },
              params
            )
          )
        }).then(({data}) => {
          console.log(data)
          this.dataList = data.data.records
          this.page.total = data.data.total
          this.dataListLoading = false
        })
      },
      // 新增 / 修改
      audit (id) {
        this.$router.push({
          path: '/jobInfo',
          query: {id: id}
        })
      },
      /**
       * 打开充值对话框
       * @param row
       */
      addIntegral(row){
        console.info(row)
        this.$prompt('最大添加200积分', '手动添加积分', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
          inputErrorMessage: '积分不正确',
          inputPlaceholder:'请输入金额'
        }).then(({ value }) => {
          var id = row.id;
          var integral = value;
          let param = {
            id: row.id,
            integral: value
          }
          this.$http({
            url: this.$http.adornUrl('/v1/member/add/integral'),
            method: 'post',
            data: this.$http.adornData(param)
          }).then(res=>{
            if(res.data.code==0){
              this.$message({
                type: 'success',
                message: '操作成功，成功充值'+integral+"点积分"
              });
              this.getDataList(this.page, this.formData);
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });
        });

      },
      cleanIntegral (id) {
        console.log(id)
        this.$confirm(`确定进行清空积分操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            let param = {
              id: id
            }
            this.$http({
              url: this.$http.adornUrl(`/v1/member/clean/integral`),
              method: 'post',
              data: this.$http.adornData(param)
            }).then(({data}) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList(this.page)
                }
              })
            })
          })
          .catch(() => {
          })
      },
      // 删除
      setManager (id,status) {
        this.$confirm(`确定进行设置操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            let param = {
              id: id,
              status: status
            }
            this.$http({
              url: this.$http.adornUrl(`/v1/member/set/manager`),
              method: 'post',
              data: this.$http.adornData(param)
            }).then(({data}) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList(this.page)
                }
              })
            })
          })
          .catch(() => {
          })
      },
      // 条件查询
      searchChange (params, done) {
        done()
        this.getDataList(this.page, params)
      },
      refreshList(){
        this.getDataList(this.page,this.dataForm)
      }
    }
  }
</script>

<style lang="scss">
.button-spacing {
  margin-top: 5px;
  margin-right: 10px;
}
</style>
